function Ajax(obj) {
  return new Promise(
    function(resolve, reject) {
      try {
        let options = {
          method: obj.method.toUpperCase() || 'GET',
          url: '',
          data: null,
          ...obj
        }
        if (!options.url) {
          throw new Error('url is need ...')
        }
        const xmlhttp = window.XMLHttpRequest ? new XMLHttpRequest() : new ActiveXObject("Microsoft.XMLHTTP");
        xmlhttp.open(options.method, options.url, true);
        if (options.method !== 'GET') {
          xmlhttp.setRequestHeader("Content-type","application/json");
          if (obj.header) {
            if (Object.prototype.toString.call(obj.header) !== '[object Object]') {
              throw new Error('header typeof should is Object')
            }
            for (const key in obj.header) {
              xmlhttp.setRequestHeader(key, obj.header[key]);
            }
          }
        }
        xmlhttp.send(options.data && JSON.stringify(options.data));
        xmlhttp.onreadystatechange = function () {
          if (xmlhttp.readyState==4 && xmlhttp.status==200) {
            resolve(JSON.parse(xmlhttp.responseText));
          }
        }
      } catch (error) {
        reject(error)
      }
    }
  )
}

export default Ajax;